package com.xiaodimall.beforesys.controller;

import com.xiaodimall.beforesys.pojo.ShoppingCartItem;
import com.xiaodimall.beforesys.pojo.User;
import com.xiaodimall.beforesys.service.CartService;
import com.xiaodimall.common.Constants;
import com.xiaodimall.common.ServiceResultEnum;
import com.xiaodimall.utils.Result;
import com.xiaodimall.utils.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


/**
 * @author wyc
 * @description
 * @ClassName CartController
 * @date 2022/11/26 14:48
 */
@Controller
public class CartController {

    //注入业务层对象
    @Resource
    CartService cartService;



    /**
     * 查询购物车中商品
     * @author WYC
     * @param request
     * @param httpSession
     * @return: 返回购物车页面
     * @date: 2022/12/1 20:46
     */
    @ApiOperation(value = "查询购物车中商品")
    @GetMapping("/shop-cart")
    public String getCartMsg(HttpServletRequest request, HttpSession httpSession) {
        return cartService.getCartMsg(request,httpSession);
    }



    /**
     * 添加商品到购物车
     * @author WYC
     * @param shoppingCartItem
     * @param httpSession
     * @return: 结果集
     * @date: 2022/11/28 17:13
     */
    @ApiOperation(value = "添加商品到购物车")
    @PostMapping("/shop-cart")
    @ResponseBody
    public Result addGoodsCart(@RequestBody ShoppingCartItem shoppingCartItem,
                                       HttpSession httpSession) {
        //调用业务层添加商品到购物车的方法
        String saveResult = cartService.addGoodsCart(shoppingCartItem,httpSession);
        //添加成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(saveResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //添加失败
        return ResultGenerator.genFailResult(saveResult);
    }


    /**
     * 根据id修改购物车商品数量
     * @author WYC
     * @param shoppingCartItem
     * @param httpSession
     * @return: 结果集
     * @date: 2022/11/28 19:49
     */
    @ApiOperation(value = "修改购物车数量")
    @PutMapping("/shop-cart")
    @ResponseBody
    public Result UpdCartGoodNum(@RequestBody ShoppingCartItem shoppingCartItem, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute(Constants.MALL_USER_SESSION_KEY);
        //判断是否登录超时
        if (user == null) {
            return ResultGenerator.genFailResult(ServiceResultEnum.SESSION_ERROR_NULL.getResult());
        }
        shoppingCartItem.setUserId(user.getUserId());
        //调用修改数量方法
        String updateResult = cartService.updateCartItem(shoppingCartItem);
        //修改成功
        if (ServiceResultEnum.SUCCESS.getResult().equals(updateResult)) {
            return ResultGenerator.genSuccessResult();
        }
        //修改失败
        return ResultGenerator.genFailResult(updateResult);
    }



    /**
     * 根据id删除购物车商品
     * @author WYC
     * @param shoppingCartItemId
     * @param session
     * @return: 结果集
     * @date: 2022/11/28 19:50
     */
    @ApiOperation(value = "删除购物车指定商品")
    @DeleteMapping("/shop-cart/{shoppingCartItemId}")
    @ResponseBody
    public Result DelCartGood(@PathVariable("shoppingCartItemId") Long shoppingCartItemId,
                                         HttpSession session) {
        return cartService.DelCartGood(shoppingCartItemId,session);
    }



    /**
     * 提交订单
     * @author WYC
     * @param request
     * @param httpSession
     * @return: 返回到提交订单页面
     * @date: 2022/12/1 20:47
     */
    @ApiOperation(value = "购物车点击去结算")
    @GetMapping("/shop-cart/settle")
    public String settlePage(HttpServletRequest request, HttpSession httpSession) {
        return cartService.toSettlePage(request,httpSession);
    }
}
